home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C013.ZIP / HEAD.C < prev    next >
Text File  |  1990-01-19  |  2KB  |  70 lines

  1. /********************************************************************
  2.  * C Users Group (U.K) C Source Code Library File CUGLIB.013        *
  3.  * Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd.         *
  4.  * Edgbaston, Birmingham B15 2QN ENGLAND                *
  5.  ********************************************************************
  6.  * File name: head.c
  7.  * Program name:head 
  8.  * Source of file: Ron Wellstead
  9.  * Purpose: An MS-DOS copy of the UNIX utility of the same name.
  10.  * Changes: <who what when & why major changes have been made>      
  11.  ********************************************************************/
  12.  
  13. /*
  14.  *
  15.  *    @(#) head.c 1.2 87/07/27 
  16.  *
  17.  *    UNIX style head utility for dos
  18.  *
  19.  *    copyright (c) 1987 Ron Wellsted.
  20.  *    This software is provided on the understanding that it is
  21.  *    NOT to be used for commercial gain. It may be freely distributed
  22.  *    in source or object form among amateur and hobby computer users ONLY!
  23.  *
  24.  *    copies the first n lines of files (or stdin) to stdout.
  25.  *    n defaults to 10
  26.  *    usage:    head [-nnn] [files...]
  27.  *    written for Microsoft C, link with setargv.obj to expand wildcards
  28.  */
  29. #include    <stdio.h>
  30.  
  31. char what[]="@(#) head VR 1.0.0 15 Jul 1987";
  32.  
  33. int count;
  34.  
  35. main(argc,argv)
  36. int argc;
  37. char *argv[];
  38. {
  39.     FILE *fp;
  40.  
  41.     if ((argc>1)&&(*argv[1]=='-')) {
  42.         --argc;
  43.         count=abs(atoi(*++argv));
  44.     } else count=10;
  45.     if (argc==1)    /* no args; copy standard input */
  46.         filecopy(stdin);
  47.     else
  48.         while (--argc>0)
  49.             if ((fp=fopen(*++argv,"r"))==NULL) {
  50.                 fprintf(stderr,"head: can't open %s\n",*argv);
  51.                 exit(1);
  52.             } else {
  53.                 filecopy(fp);
  54.                 fclose(fp);
  55.             }
  56.     exit(0);
  57. }
  58.  
  59. filecopy(fp)
  60. FILE *fp;
  61. {
  62.     int c,lines=1;
  63.  
  64.     while ((c=getc(fp))!=EOF) {
  65.         putchar(c);
  66.         if (c=='\n') ++lines;
  67.         if (lines==count) break;
  68.     }
  69. }
  70.